package com.gitee.chili.leisure.message;

import com.gitee.chili.leisure.core.AbstractDefaultMessage;
import com.gitee.chili.leisure.core.Packet;
import com.gitee.chili.leisure.model.BaseMessageModel;
import com.google.protobuf.InvalidProtocolBufferException;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

/**
 * netty 心跳消息
 *
 * @author week solider
 * @since 2020.08.31
 */
@Slf4j
public class Heartbeat extends AbstractDefaultMessage {


    public Heartbeat(Packet packet, Channel channel) {
        super(packet, channel);
    }

    @Override
    public void decoder(byte[] body) throws InvalidProtocolBufferException {
        log.debug("收到心跳数据 time: {}", System.currentTimeMillis());
        BaseMessageModel.HeartbeatCmd heartbeatCmd = BaseMessageModel.HeartbeatCmd.parseFrom(body);
    }

    @Override
    public Packet parseBody() {
        long currentTimeMillis = System.currentTimeMillis();
        log.debug("响应心跳数据 time: {}", currentTimeMillis);
        BaseMessageModel.HeartbeatResult.Builder builder = BaseMessageModel.HeartbeatResult.newBuilder();
        builder.setTimestamp(currentTimeMillis);
        return Packet.newPacket(getPacket().getCmd(), builder.build().toByteArray());
    }

}
